package  nc.ui.zzjs.price;

import nc.ui.pub.beans.UIDialog;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.trade.controller.IControllerBase;
import nc.ui.zzjs.grandbill.GrandBillEventHandler;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.VOStatus;
import nc.vo.zzjs.price.SettlePriceBVO;
import nc.vo.zzjs.price.SettlePriceGsVO;


public class ClientEventHandler extends GrandBillEventHandler {

	public ClientEventHandler(ClientUI billUI, IControllerBase control) {
		super(billUI, control);
	}

	@Override
	protected UIDialog createQueryUI() {
		return new ClientUIQueryDlg(getBillUI(), null,
				_getCorp().getPrimaryKey(), getBillUI().getModuleCode(),
				_getOperator(), null, null);
	}
	
	
	@Override
	protected void onBoSave() throws Exception {
		doBeforeSave();
		super.onBoSave();
	}
	
	private void doBeforeSave() throws Exception{
		AggregatedValueObject checkVO = getBillUI().getChangedVOFromUI();
		if(checkVO==null)
			return;
		SettlePriceBVO[] bvos =(SettlePriceBVO[])checkVO.getChildrenVO();
		if(bvos==null || bvos.length==0)
			return;
		for(SettlePriceBVO bvo:bvos){
			bvo.validate();
		}
	}
	
	protected void onBoCopy() throws Exception {
		super.onBoCopy();
		AggregatedValueObject copyVo = getBufferData().getCurrentVOClone();
		SettlePriceBVO[] bvos = (SettlePriceBVO[])copyVo.getChildrenVO();
		 clearItemValue(bvos);
		getBillCardPanel().getHeadItem("vcode").setValue(null);
		getBillCardPanel().getHeadItem("vname").setValue(null);
		getBillCardPanel().getHeadItem("voperatorid").setValue(_getOperator());
	}
	
	private BillCardPanel getBillCardPanel(){
		return getBillCardPanelWrapper().getBillCardPanel();
	}
	
	public void clearItemValue(SettlePriceBVO[] bvos){
		if(bvos == null || bvos.length==0)
			return;
		for(SettlePriceBVO bvo:bvos){
			SettlePriceGsVO[] gsvos =(SettlePriceGsVO[])bvo.getGrandsonsupervos();
			for(SettlePriceGsVO gsvo:gsvos){
				gsvo.setPk_settleprice_b(null);
				gsvo.setPk_settleprice_gs(null);
				gsvo.setStatus(VOStatus.NEW);
			}
		}
	}
}
